﻿using Nemesis.FiniteStateMachine;
using Nemesis.Plugins.NRelog.Controllers;
using Nemesis.Plugins.NRelog.Settings;
using Nemesis.WoW.ObjectManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Nemesis.Plugins.NRelog.States
{
    internal class LogoutState : State
    {
        public override string DisplayName
        {
            get { return "RunningState"; }
        }

        public override bool NeedToRun
        {
            
            get 
            {
                if (DateTime.Now > SchedulerSettings.endTime && !ObjectManager.Me.InCombat && ObjectManager.Me.IsIngame)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public override int Priority
        {
            get { return 10000; }
        }

        public override void Run()
        {
            Nemesis.Bot.Engine.Pause();
            Logger.WriteLine("[NRelog] End time reached. We will log out in 5 Seconds.");
            Thread.Sleep(5000);
            Nemesis.WoW.Tools.Logout.LogMeOut();
            if (SchedulerSettings.relogEnabled && SchedulerSettings.relogCount > SchedulerSettings.timesReloged)
            {
                Logger.WriteLine("[NRelog] We will relog at " + SchedulerSettings.restartTime.ToString());
                SchedulerSettings.isOnPause = true;
                PauseThread thread = new PauseThread();
            }
            else
            {
                Logger.WriteLine("[NRelog] We won't relog!");
                Nemesis.Bot.Engine.Stop(); 
            }
            
            
        }
    }
}
